﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using Maticsoft.Common;

namespace Maticsoft.Web
{
    public partial class UserOrder : PageBaseMember
    {
        int _Status;
        protected void Page_Load(object sender, EventArgs e)
        {
            Check();

            AjaxProcess();

            UserLeft1.FlagId = "3";
            int.TryParse(Request.QueryString["status"], out _Status);
            if (!IsPostBack)
            {
                BindList();
            }
        }

        protected void AjaxProcess()
        {
            if (Request.QueryString["op"] == "confirm")
            {
                int orderId = DigitHelper.GetInt32(Request.QueryString["orderId"]);
                if (orderId > 0)
                {
                    BLL.Orders ordBLL = new BLL.Orders();
                    Model.Orders ordModel = ordBLL.GetModel(orderId);
                    if (ordModel.UserId == DigitHelper.GetInt32(this.UserId))
                    {
                        ordModel.Status = 5;
                        ordModel.UpdateTime = DateTime.Now;
                        ordBLL.Update(ordModel);
                        Response.Write("1");
                    }
                }

                Response.End();
            }
            else if (Request.QueryString["op"] == "pay")
            {
                int orderId = DigitHelper.GetInt32(Request.QueryString["orderId"]);
                if (orderId > 0)
                {
                    BLL.Orders ordBLL = new BLL.Orders();
                    Model.Orders ordModel = ordBLL.GetModel(orderId);
                    if (ordModel.UserId == DigitHelper.GetInt32(this.UserId))
                    {
                        Response.Write("{\"code\":\"1\",\"msg\":\"\",\"data\":{\"orderid\":" + orderId + ",\"fee\":" + ordModel.Total + "}}");
                    }
                    else
                    {
                        Response.Write("{\"code\":\"-1\",\"msg\":\"\"}");
                    }
                }

                Response.End();
            }
            
        }

        protected void BindList()
        {
            BLL.Orders orderBLL = new BLL.Orders();
            string strWhere = "UserId=" + this.UserId + " and IsActive=1";
            if (_Status == 1)
            {
                strWhere += " and Status=1";
            }
            else if (_Status == 2)
            {
                strWhere += " and Status in(2,3,4)";
            }
            int recordCount = orderBLL.GetRecordCount(strWhere);
            DataSet ds = orderBLL.GetListByPageV(strWhere, "orderId desc", AspNetPager1.StartRecordIndex, AspNetPager1.EndRecordIndex);
            Repeater1.DataSource = ds.Tables[0].DefaultView;
            Repeater1.DataBind();
            AspNetPager1.RecordCount = recordCount;
        }

        protected string GetHeight(int orderId)
        {
            BLL.Oitems oitemBLL = new BLL.Oitems();
            DataSet ds = oitemBLL.GetList("OrderId=" + orderId);
            int num = ds.Tables[0].Rows.Count;
            if (num == 1)
            {
                return "height:" + (num * 60).ToString() + "px;";
            }
            else
            {
                return "height:" + ((num * 60) + (num - 1) * 45) + "px;";
            }
        }

        protected string GetOitem(int orderId)
        {
            BLL.Oitems oitemBLL = new BLL.Oitems();
            DataSet ds = oitemBLL.GetList("OrderId=" + orderId);
            StringBuilder strBuilder = new StringBuilder();
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                strBuilder.Append("<div class=\"goods-loop\">");
                strBuilder.Append("<div class=\"equal-2 fl \">");
                strBuilder.Append("<p class=\"equal-1 fl\"><img class=\"borderbox\" src=\"uploadpic/" + dr["ProductPic"] + "\" style=\"width:82px;height:82px;\"></p>");
                strBuilder.Append("<div class=\"equal-3 fl tc-black pad-10-10\" style=\"width: 71%;\">");
                strBuilder.Append("<p style=\"height:60px\">" + dr["ProductName"] + "</p>");
                strBuilder.Append("</div>");
                strBuilder.Append("</div>");
                strBuilder.Append("<div class=\"equal-2 fl\">");
                strBuilder.Append("<div class=\"equal-1-3 fs-12 tc-black fl tc\">"+dr["RealPrice"]+"</div>");
                strBuilder.Append("<div class=\"equal-1-3 fs-12 tc-black fl tc\">" + dr["ProductNum"] + "</div>");
                strBuilder.Append("<div class=\"equal-1-3 fs-12 tc-black fl tc\"></div>");
                strBuilder.Append("</div></div>");
            }

            return strBuilder.ToString();
        }

        protected string GetOperation(int orderId, int state)
        {
            string result = string.Empty;
            if (state == 1)
            {
                //result += "<a href='javascript:;' onclick='Pay(" + orderId + ")'>去支付</a>";
                result += "<a href='/pay/alipay/default.aspx?orderid=" + orderId + "&src=orderlist' target='_blank'>去支付</a>";
            }
            else if (state == 2 || state==3 || state==4)
            {
                result += "<a href='javascript:;' onclick='Confirm(" + orderId + ");'>确认收货</a>";
            }
            else if (state == 5)
            {
                result += "<a href='UserComments.aspx?orderId=" + orderId + "'>待评价</a>";
            }
            else if (state == 6)
            {
                result += "<a href='javascript:;'>已评价</a>";
            }

            return result;
        }

        protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            AspNetPager1.CurrentPageIndex = e.NewPageIndex;
            BindList();
        }
    }
}